<template>
  <div class="userOuter">
    <!-- 面包屑导航 -->
    <div class="userTop">
      <el-breadcrumb :separator-icon="ArrowRight">
        <el-breadcrumb-item :to="{ path: '/layout/portal' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      </el-breadcrumb>
    </div>

    <!-- 标题 -->
    <h3 class="title">用户管理</h3>

    <!-- 内容区 -->
    <div class="user">
      <div class="user_top">
        <!-- 搜索用户 -->
        <div class="user_search">
          <el-input v-model="keyWord" placeholder="请输入用户名" clearable />
          <el-button type="primary" plain @click="searchUser">搜索</el-button>
        </div>
      </div>
      <!-- 用户列表 -->
      <div class="user_list">
        <el-table height="460" :data="tableData" stripe style="width: 100%">
          <el-table-column align="center" type="index" index="index" width="60" />
          <el-table-column align="center" prop="username" label="用户名" width="120" />
          <el-table-column align="center" label="博主权限" width="120">
            <template #default="scope">
              <el-switch size="small" v-model="scope.row.isBlogger"
                @change="upUser(scope.row._id, scope.row.isBlogger)" />
            </template>
          </el-table-column>
          <el-table-column align="center" prop="eMail" label="邮箱" width="200" />
          <el-table-column align="center" prop="createTime" label="注册时间" width="160" />
          <el-table-column align="center" label="操作" width="200">
            <template #default="scope">
              <el-button type="danger" :icon="Delete" circle @click="delUser(scope.row._id)" />
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted } from "vue";
import { ArrowRight, Delete, Edit } from "@element-plus/icons-vue";
import { ElMessage } from "element-plus";
import { timeChange } from '../../hooks/index'

import {
  getUserInfoApi,
  searchUserApi,
  upUserApi,
  delUserApi,
} from "../../service/apiModule/userManageApi";

onMounted(() => {
  getUserInfo();
});

// 用户列表数据
const tableData = ref([
  {
    eMail: "",
    isBlogger: false,
    username: "",
    createTime: 0,
  },
]);

// 用户搜索关键字
const keyWord = ref<String>("");

// 用户列表序号
const indexMethod = (index: number) => {
  return index;
};

// 获取用户信息
const getUserInfo = async () => {
  const { data: res } = await getUserInfoApi();
  if (res.status.code !== 200) {
    ElMessage.error("查询失败!");
  } else {
    res.data.forEach((item: any) => {
      item.createTime = timeChange(item.createTime)
    })
    ElMessage.success("用户查询成功!");
    tableData.value = res.data;
  }
};

// 用户搜索
const searchUser = async () => {
  if (keyWord.value === "") {
    getUserInfo();
  } else {
    const { data: res } = await searchUserApi({
      username: keyWord.value,
    });
    if (res.status.code !== 200) {
      ElMessage.error("搜索失败!");
    } else {
      res.data.forEach((item: any) => {
        item.createTime = timeChange(item.createTime)
      })
      tableData.value = res.data;
    }
  }
};

// 删除用户
const delUser = async (data: any) => {
  const { data: res } = await delUserApi({ id: data });
  if (res.status.code !== 200) {
    ElMessage.error("删除失败!");
  } else {
    ElMessage.success("删除成功!");
    getUserInfo();
  }
};

// 修改权限
const upUser = async (id: String, status: Boolean) => {
  const { data: res } = await upUserApi({ id, status });
  if (res.status.code !== 200) {
    ElMessage.error("修改失败!");
  } else {
    ElMessage.success("修改成功!");
    getUserInfo();
  }
};
</script>

<style lang="scss" scoped>
$H: 91vh;

.userOuter {
  width: 100%;
  height: $H;
  background-color: rgb(243, 243, 243);
  padding: 20px;

  .userTop {
    width: 100%;
    background-color: rgba(255, 255, 255, 0.6);
    padding: 10px 10px;
    border-radius: 10px;
  }

  .title {
    margin-top: 20px;
    margin-bottom: 20px;
  }

  .user {
    background-color: #fff;
    padding: 10px 0 0 10px;
    border-radius: 10px;
    min-height: 60%;
    max-height: 86%;

    .user_top {
      width: 100%;
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 10px 30px 10px 10px;

      .user_search {
        width: 26%;
        display: flex;
        align-items: center;

        &>.el-input {
          margin-right: 6px;
        }
      }
    }
  }
}
</style>
